<?php
include_once("KillCommonModel.class.php");
include_once("Order.class.php");
include_once("Goods.class.php");
class RedisKillModel extends KillCommonModel{

    private $redis=null;
    //redis链接地址
    private $ip="127.0.0.1";

    private $port="6379";

    /**
     * 链接redis
     * connectRedis
     * @param string $ip
     * @param string $port
     */
    public function connectRedis($ip="",$port="")
    {
        $this->redis=new Redis();
        $this->redis->connect($this->ip,$this->port);
    }

    /**
     * 抢购入口
     * @param $id
     * @param $userId
     */
     public function RunPurchase($id,$userId){
            $this->goodsIdBy=$id;
            $this->userId=$userId;
             if(!$this->run()){
                 return false;
             }
     }

    /**
     * 初始化商品库存
     * @param $id
     * @return bool
     */
    public function manageRedisCount($id){

        $this->checkClassExists();
        $this->connectRedis();
        $goodsObj=$this->goodsObj;
        $where=array($this->goodsId=>$id);
        $goodsInfo=$goodsObj->getRowGoodsInfo($where);

        if($goodsInfo==null || !$goodsInfo){
            $this->error="商品不存在";
            return false;
        }
        if($goodsInfo[$this->counts]<1){
            $this->error="商品没有库存";
            return false;
        }
        $key 	= 'goods_list_'.$id;
        if($count=$this->redis->llen($key)){
                for($i=1;$i<=$count;$i++){
                    $this->redis->lpop($key);
                 }
        }
        for($i=1;$i<=$goodsInfo[$this->counts];$i++){
           echo  $this->redis->lpush($key,1);
        }
    }

    private function run(){
        if(!$this->checkClassExists()){
            return false;
        }
        if($this->connectRedis()){
         return false;
        }
        if(!$this->manage()){
            return false;
        }
    }

    public function setConfig($ip,$port){
        $this->ip=$ip;
        $this->port=$port;
    }

    //处理抢购的并发问题
    private function manage(){

        $goodsObj=$this->goodsObj;
        $where=array($this->goodsId=>$this->goodsIdBy);
        $goodsInfo=$goodsObj->getRowGoodsInfo($where);

        if($goodsInfo==array() || !$goodsInfo){
            $this->error="商品不存在";
            return false;
        }
        if($this->checkUserBuyStatus()){
            $this->error="你已经抢购过，该商品。";
            return false;
        }

        if(!$this->checkGoodsCountExists()){
            $this->error="库存已完";
            return false;
        }

        $resul=$this->oderObj->createOrder($goodsInfo,$this->userId);
        if($resul){
            $result=$this->goodsObj->updateCount($goodsInfo);
            if($result){
                $this->error="抢购成功";
                $this->setUserBuyInfo();
                $this->redis->close();
                return true;
            }
        }
        $key 	= 'goods_list_'.$this->goodsIdBy;
        $this->redis->lpush($key,1);
        $this->error="网络超时";
        return false;
    }

    private function setUserBuyInfo(){
        $key="'goods_list_".$this->goodsIdBy."_".$this->userId."'";
       $this->redis->set($key,$this->userId);
    }

    /*
     * 检查商品的库存是否还存在
     * @return bool
     */
    private function checkGoodsCountExists(){
        $key 	= 'goods_list_'.$this->goodsIdBy;
        $count 	= $this->redis->lpop($key);
        return $count;
    }

    /*
     * 检查该用户是否已经购买该商品
     * @return bool
     */
    private function checkUserBuyStatus(){
        $key="'goods_list_".$this->goodsIdBy."_".$this->userId."'";
        $value=$this->redis->exists($key);

        return $value;
    }

    /*
     * 检查初始化配置是否存在
     * @return bool
     */
    private function checkClassExists(){

        if(!class_exists($this->goodsObj)){
            $this->error="商品表类不存在";
            return false;
        }
        $this->goodsObj=$goodsObj=new $this->goodsObj();
        if(!method_exists($goodsObj,"getRowGoodsInfo")){
            $this->error="商品表类中getRowGoodsInfo方法不存在";
            return false;
        }
        if(!method_exists($goodsObj,"updateCount")){
            $this->error="商品表类中updateCount方法不存在";
            return false;
        }

        if(!class_exists($this->oderObj)){
            $this->error="订单表类不存在";
            return false;
        }

        $this->oderObj=$orderObj=new $this->oderObj();
        if(!method_exists($orderObj,"createOrder")){
            $this->error="订单表类中createOrder方法不存在";
            return false;
        }
        return true;
    }
}